package com.hullunyingshi.until;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 类 名: StringTools 字符串处理拓展类
 * 
 * @version V1.0
 * @date 2018年3月21日
 * @author liyanli * @QQ 792442257
 *
 */
public class StringTools {

	/**
	 * 判断字符串内容是否为无效数据
	 *
	 * @param obj
	 * @return
	 */
	public static boolean strHasNoData(Object obj) {
		if (obj != null && !"".equals(String.valueOf(obj).replaceAll(" ", ""))
				&& !"null".equalsIgnoreCase(String.valueOf(obj))) {
			return false;
		}
		return true;
	}

	/**
	 * 获取随机数字符串
	 *
	 * @param count 需要随机数个数
	 * @return String
	 */
	public static String makeRandomNumStr(int count) {
		String returnStr = "";
		for (int i = 0; i < count; i++) {
			Random rand = new Random();
			returnStr += String.valueOf(rand.nextInt(10));
		}
		return returnStr;
	}

	/**
	 * 判断字符串类型是否可以转换为数字类型
	 * 
	 * @param str 目标字符串
	 * @return 判断结果
	 * @author qixin
	 */
	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	/**
	 * 获取异常的详细信息
	 *
	 * @author huangwei
	 * @param ex
	 * @return
	 */
	public static String getExceptionAllInfo(Exception ex) {
		ByteArrayOutputStream out = null;
		PrintStream pout = null;
		String ret = "";
		try {
			out = new ByteArrayOutputStream();
			pout = new PrintStream(out);
			ex.printStackTrace(pout);
			ret = new String(out.toByteArray());
			out.close();
		} catch (Exception e) {
			return ex.getMessage();
		} finally {
			if (pout != null) {
				pout.close();
			}
		}
		return ret;
	}

	/**
	 * 获取UUID
	 * 
	 * @return
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	/**
	 * 获取当前时间戳，单位秒
	 * 
	 * @return
	 */
	public static long getCurrentTimestamp() {
		return System.currentTimeMillis() / 1000;
	}

	/**
	 * 获取当前时间戳，单位毫秒
	 * 
	 * @return
	 */
	public static long getCurrentTimestampMs() {
		return System.currentTimeMillis();
	}

	/**
	 * 获取随机数字符串带时间
	 *
	 * @param count 需要随机数个数
	 * @return String
	 */
	public static String makeRandomNumStrWithDateTime(int count) {
		String returnStr = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		for (int i = 0; i < count; i++) {
			Random rand = new Random();
			returnStr += String.valueOf(rand.nextInt(10));
		}
		return sdf.format(new Date()) + returnStr;
	}



	/*
	 * public static void main(String[] args) throws Exception {
	 * System.out.println(createOrderNo()); }
	 */
}
